//
//  QCAutoBalanceController.swift
//  爱汽车
//
//  Created by tucici on 2024/2/1.
//

import UIKit

class QCAutoBalanceController: QCViewController {
    var array = [["气压平衡"],["气压优先","高度优先"]]
    var values = [[false],[true,false]]
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("气压平衡")
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSettingCell.nib(), forCellReuseIdentifier: QCSettingCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    
    override var bytes: [UInt8]{
        didSet{
            let ack = bytes[7]
            if ack != UInt8(AIQC.userInfo.ack){return}
            let data = [UInt8](bytes[AI_index..<bytes.count])
            if recived == false {
                recived = true
                return
            }
            values[0][0] = data[96] == 1
            values[1][0] = data[97] == 0
            values[1][1] = data[97] == 1
            tableView.reloadData()
        }
    }
    
}
//MARK: - UITableViewDataSource
extension QCAutoBalanceController:UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return values[0][0] ? array.count : 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCSettingCell.ID()) as! QCSettingCell
        let bool = values[indexPath.section][indexPath.row]
        cell.title.text = LS(array[indexPath.section][indexPath.row])
        cell.type = .switchBtn(bool)
        cell.type = indexPath.section == 0 ? .switchBtn(bool) : .subImg(bool ? "选择" : "未选择")
        cell.switchBtn.isHidden = indexPath.section == 1
        cell.block = {[weak self] ison in
            self?.recived = false
            self?.values[indexPath.section][indexPath.row] = ison
            self?.tableView.reloadData()
            BLEData.appSetDataToPer(AIQC.autoFlatSwitch.cmd, ison ? "01" : "00").write
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCAutoBalanceController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0{return}
        values[1][0] = indexPath.row  == 0
        values[1][1] = indexPath.row  == 1
        tableView.reloadData()
        BLEData.appSetDataToPer(AIQC.autoFlatLevel.cmd, indexPath.row  == 0 ? "00" : "01").write
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kUIScreenWidth, height: 10))
        view.bgHexColor = "#161718"
        return view
    }
}
